<template>
  <div class="table-box">
    <ProTable ref="proTableRef" row-key="id" title="列表" :columns="columns" :request-api="loadTable" highlight-current-row>
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <ElButton type="primary" :icon="CirclePlus" @click="onAddClicked">新增</ElButton>
      </template>
      <!-- 账户操作 -->
      <template #operation="scope">
        <ElButton type="primary" link :icon="View" @click="onMapClicked(scope.row)">预览</ElButton>
        <ElButton type="primary" link :icon="View" @click="onViewClicked(scope.row)">查看</ElButton>
        <ElButton type="success" v-if="scope.row.active === 0" link :icon="EditPen" @click="onActiveClicked(scope.row, 1)">
          启用
        </ElButton>
        <ElButton type="danger" v-if="scope.row.active === 1" link :icon="View" @click="onActiveClicked(scope.row, 0)">
          停用
        </ElButton>
        <ElButton type="warning" link :icon="EditPen" @click="onEditClicked(scope.row)">编辑</ElButton>
        <ElButton type="danger" link :icon="Delete" @click="onDeleteClicked(scope.row)">删除</ElButton>
      </template>
    </ProTable>
    <el-image-viewer v-if="imgViewVisible" :url-list="[currentImage]" @close="imgViewVisible = false" />
    <LayerConfigForm ref="formRef" @success="refresh" />
  </div>
</template>

<script setup lang="ts" name="ZLLayerConfig">
import { CirclePlus, Delete, EditPen, View } from '@element-plus/icons-vue'
import LayerConfigForm from './layer-config-form.vue'
import ProTable from '@/components/ProTable/index.vue'
import { ColumnProps, ProTableInstance } from '@/components/ProTable/interface'
import { ref, reactive } from 'vue'
import { LayerConfig, pageLayerConfig, removeLayerConfig, activeLayerConfig } from '@api/tby/common/layerConfig'
import { ElMessage, ElMessageBox } from 'element-plus'
import { FormType } from '@/enums'

const proTableRef = ref<ProTableInstance>()
const imgViewVisible = ref(false)
const currentImage = ref('')

function refresh() {
  proTableRef.value?.getTableList()
}

function getIndex(index: number) {
  const { current, size } = proTableRef.value!.pageable
  return (current - 1) * size + index + 1
}

// 表格列
const columns = reactive<ColumnProps<LayerConfig.Record>[]>([
  { type: 'index', label: '序号', width: 60, index: getIndex },
  { prop: 'name', label: '图层名', search: { el: 'input', key: 'name', order: 1 }, align: 'center', minWidth: 200 },
  { prop: 'url', label: '图层地址', align: 'center', minWidth: 200 },
  {
    prop: 'active',
    label: '是否启用',
    enum: LayerConfig.ActiveEnum,
    tag: true,
    search: { el: 'select', key: 'active', order: 2 }
  },
  { prop: 'operation', label: '操作', fixed: 'right', width: 350 }
])

const loadTable = (query: LayerConfig.Query) => {
  query.order = 'create_time desc'
  return pageLayerConfig(query)
}

const formRef = ref<InstanceType<typeof LayerConfigForm> | null>(null)

// 添加
const onAddClicked = () => {
  formRef.value?.open(FormType.ADD)
}

// 修改
const onEditClicked = (row: any) => {
  formRef.value?.open(FormType.EDIT, row.id)
}

// 查看用户
const onViewClicked = (row: any) => {
  formRef.value?.open(FormType.VIEW, row.id)
}

// 预览查看
const onMapClicked = (row: any) => {
  currentImage.value = row.icon
  imgViewVisible.value = true
}

const onActiveClicked = (row: any, active: number) => {
  let message = '当前图层已启用，如停用会导致底图无法加载，是否停用?'
  let successMessage = '设置成功，请尽快启用其他图层，以防底图无法加载'
  if (active === 1) {
    message = '启用该图层，将自动停用目前已配置图层的,是否继续?'
  }
  ElMessageBox.confirm(message, '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    activeLayerConfig(row.id, active).then(response => {
      console.log(response)
      ElMessage({
        type: 'success',
        message: successMessage
      })
      proTableRef.value?.getTableList()
    })
  })
}

// 删除
const onDeleteClicked = (row: any) => {
  ElMessageBox.confirm('是否删除数据?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    removeLayerConfig(row.id).then(response => {
      console.log(response)
      ElMessage({
        type: 'success',
        message: '删除成功!'
      })
      proTableRef.value?.getTableList()
    })
  })
}
</script>
